package com.bwie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.bwie.common.constants.LoginConstants;
import com.bwie.common.domaintoo.Type;
import com.bwie.common.domaintoo.request.CouponAdd;
import com.bwie.common.domaintoo.request.CouponRequest;
import com.bwie.common.domaintoo.response.CouponResponse;
import com.bwie.common.result.Result;
import com.bwie.common.utils.OssUtil;
import com.bwie.exception.CouponException;
import com.bwie.mapper.CouponMapper;
import com.bwie.service.CouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @className: MenuServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: 张雨乐
 * @date: 2025/9/27 19:36
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    OssUtil ossUtil;

    @Autowired
    CouponMapper couponMapper;

    @Override
    public Result list(CouponRequest couponRequest) {
        PageHelper.startPage(couponRequest.getPageNum(),couponRequest.getPageSize());
        List<CouponResponse>list=couponMapper.list(couponRequest);
        PageInfo<CouponResponse> info = new PageInfo<>(list);
        return Result.success(info);
    }

    @Override
    public Result couponAdd(CouponAdd couponAdd) {
        String couponName = couponAdd.getCouponName();
        String couponType = couponAdd.getCouponType();
        Integer discountValue = couponAdd.getDiscountValue();
        String receive = couponAdd.getReceive();
        if (StrUtil.isBlank(couponName)){
            throw new CouponException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (StrUtil.isBlank(couponType)){
            throw new CouponException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (discountValue==null){
            throw new CouponException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        if (StrUtil.isBlank(receive)){
            throw new CouponException(LoginConstants.ERROR,LoginConstants.ERROR_MSG);
        }
        couponMapper.couponAdd(couponAdd);
        return Result.success("添加成功");
    }


    @Override
    public Result img(MultipartFile file) {
        String s = ossUtil.uploadMultipartFile(file);
        return Result.success(s);
    }

    @Override
    public Result typeList(String id) {
        List<Type>list=couponMapper.typeList(id);
        return Result.success(list);
    }
}
